package com.oanda.blackberry.restapi;

import java.io.IOException;

import net.rim.device.api.system.DeviceInfo;

import org.json.me.JSONException;
import org.json.me.JSONObject;

import com.oanda.blackberry.net.ConnectionManager;
import com.oanda.blackberry.net.RestException;
import com.oanda.blackberry.net.UrlEncoder;
import com.oanda.blackberry.util.Model;
import com.oanda.blackberry.util.log.Logger;

public class User extends Rest {
    String session_token;
    String username;
    String password;
    String email;

    public void sendOtp(String username, String sessionToken) throws RestException {
        String url = ConnectionManager.getUrl() + "v1/user/send_otp.json";

        String parameters = "";
        try {
            parameters = "?username=" + UrlEncoder.encode(username, "") + "&session_token="
                    + UrlEncoder.encode(sessionToken, "");

            url = url + ConnectionManager.getConnectionSuffix();

            this.response = proceedConnection(url, parameters.substring(1));

            if (this.error) {
                Logger.log(this.errorMessage == null ? "" : this.errorMessage);
                Logger.log(this.response == null ? "" : this.response);
                throw new RestException(this.errorMessage
                        + (this.response == null ? "" : this.response));
            }
        } catch (IOException ex) {
        }
        Logger.log("login.json: " + this.response);
    }

    private String generateLoginParams() {
        try {
            String api_key = "0325ee6232373738";
            String device_type = "BlackBerry";
            String device_version = DeviceInfo.getDeviceName();
            String client_version = Model.getVERSION();
            String operating_system = DeviceInfo.getSoftwareVersion();
            String client_type = "MOBILE";

            return "&api_key=" + UrlEncoder.encode(api_key, "") + "&device_type="
                    + UrlEncoder.encode(device_type, "") + "&device_version="
                    + UrlEncoder.encode(device_version, "") + "&client_version="
                    + UrlEncoder.encode(client_version, "") + "&operating_system="
                    + UrlEncoder.encode(operating_system, "") + "&client_type="
                    + UrlEncoder.encode(client_type, "");
        } catch (IOException ex) {
        }

        return "";
    }

    public OtpStatus discoverOtpStatus(String username, String password) throws RestException {
        OtpStatus otpStatus = new OtpStatus();
        otpStatus.isOtp = true;

        String url = ConnectionManager.getUrl() + "v1/user/login.json";

        String api_key = "0325ee6232373738";

        String parameters = "";
        try {
            parameters = "?username=" + UrlEncoder.encode(username, "") + "&password="
                    + UrlEncoder.encode(password, "") + generateLoginParams();

            url = url + ConnectionManager.getConnectionSuffix();

            this.response = proceedConnection(url, parameters.substring(1));

            if (this.error) {
                Logger.log(this.errorMessage == null ? "" : this.errorMessage);
                Logger.log(this.response == null ? "" : this.response);
                throw new RestException(this.errorMessage
                        + (this.response == null ? "" : this.response));
            }

            JSONObject json = new JSONObject(this.response);
            String sessionToken = json.getString("session_token");

            otpStatus.isOtp = false;
            otpStatus.sessionToken = sessionToken;
        } catch (IOException ex) {
        } catch (JSONException ex) {
        }
        Logger.log("login.json: " + this.response);
        return otpStatus;
    }

    public String login(String username, String sessionToken, String otp) throws RestException {
        String url = ConnectionManager.getUrl() + "v1/user/login.json";

        String parameters = "";
        try {
            parameters = "?username=" + UrlEncoder.encode(username, "") + "&session_token="
                    + UrlEncoder.encode(sessionToken, "") + generateLoginParams();

            if (otp != null) {
                parameters = parameters + "&one_time_password=" + otp;
            }
        } catch (IOException ex) {
        }

        url = url + ConnectionManager.getConnectionSuffix();

        this.response = proceedConnection(url, parameters.substring(1));

        if (this.error) {
            Logger.log(this.errorMessage == null ? "" : this.errorMessage);
            Logger.log(this.response == null ? "" : this.response);
            throw new RestException(this.errorMessage
                    + (this.response == null ? "" : this.response));
        }
        Logger.log("login.json: " + this.response);
        return this.response;
    }

    public String logout(String sessionToken) throws RestException, IOException {
        String url = ConnectionManager.getUrl() + "v1/user/logout.json";

        url = url + ConnectionManager.getConnectionSuffix();
        String parameters = "?session_token=" + UrlEncoder.encode(sessionToken, "");

        this.response = proceedConnection(url, parameters.substring(1));

        if (this.error) {
            throw new RestException(this.errorMessage
                    + (this.response == null ? "" : this.response));
        }
        Logger.log("logout.json: " + this.response);
        return this.response;
    }

    public void rateLimit() {
    }

    public String register(String username, String password, String email, boolean isReceiveEmails)
            throws RestException {
        if (ConnectionManager.getUrl().equals("device")) {
            return "{\"session_token\":\"123555123\"}";
        }
        String url = ConnectionManager.getUrl() + "v1/user/register.json";

        String parameters = "";
        try {
            parameters = "?username=" + UrlEncoder.encode(username, "") + "&password="
                    + UrlEncoder.encode(password, "") + "&email=" + UrlEncoder.encode(email, "")
                    + "&first_name=" + UrlEncoder.encode("Blackberry", "") + "&last_name="
                    + UrlEncoder.encode("Blackberry", "") + "&currency="
                    + UrlEncoder.encode("USD", "") + "&email_opt_in="
                    + UrlEncoder.encode(isReceiveEmails ? "1" : "0", "");
        } catch (IOException ex) {
        }

        url = url + ConnectionManager.getConnectionSuffix();

        this.response = proceedConnection(url, parameters.substring(1));

        if (this.error) {
            Logger.log(this.errorMessage == null ? "" : this.errorMessage);
            Logger.log(this.response == null ? "" : this.response);
            throw new RestException(this.errorMessage
                    + (this.response == null ? "" : this.response));
        }
        Logger.log("register.json: " + this.response);
        return this.response;
    }

    public void registerZero() {
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setSession_token(String session_token) {
        this.session_token = session_token;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getEmail() {
        return this.email;
    }

    public String getPassword() {
        return this.password;
    }

    public String getSession_token() {
        return this.session_token;
    }

    public String getUsername() {
        return this.username;
    }

    public class OtpStatus {
        public String sessionToken;
        public boolean isOtp;

        public OtpStatus() {
        }
    }
}
